package com.zl.gmadmin.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zl.gmadmin.common.ApiResponse;
import com.zl.gmadmin.entity.BlogArticle;
import com.zl.gmadmin.payload.BlogArticleQueryCriteria;
import com.zl.gmadmin.service.BlogArticleService;
import com.zl.gmadmin.utils.LogUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ZhangLiang
 * @Date: 2021/08/24/13:41
 * @Description:
 */
@Api(value = "博客文章管理")
@RestController
@RequestMapping("/api/blog/article")
public class BlogArticleController {
    private final BlogArticleService service;

    public BlogArticleController(BlogArticleService service) {
        this.service = service;
    }

    @ApiOperation("分页获取文章")
    @GetMapping
    public ApiResponse getPage(BlogArticleQueryCriteria queryCriteria, Page<BlogArticle> page) {
        LogUtil.debug("查询条件:"+ JSONUtil.toJsonStr(queryCriteria));
        return ApiResponse.ofSuccess(service.selectPageVo(queryCriteria, page));
    }
    @ApiOperation("新增文章")
    @PostMapping
    public ApiResponse addBlogArticle(@RequestBody BlogArticle blogArticle){
        System.out.println("新增:"+JSONUtil.toJsonStr(blogArticle));
       service.saveBlogArticle(blogArticle);
        return ApiResponse.ofSuccess();
    }

    @ApiOperation("删除博客文章")
    @DeleteMapping
    public ApiResponse delBlogArticle(@RequestBody List<Long> ids){
          service.removeByIds(ids);
        return ApiResponse.ofSuccess();
    }

    @ApiOperation("更新博客文章")
    @PutMapping
    public ApiResponse updateBlogArticle(@RequestBody BlogArticle blogArticle){
        System.out.println("更新: "+JSONUtil.toJsonStr(blogArticle));
        service.updateBlogArticle(blogArticle);
        return ApiResponse.ofSuccess();
    }

    @ApiOperation("根据id获取文章内容")
    @GetMapping("/{id}")
    public  ApiResponse getById(@PathVariable("id") Long id){
        return ApiResponse.ofSuccess(service.getById(id));
    }

}
